package com.xuan.temper.controller;

import com.alibaba.fastjson.JSON;
import com.xuan.temper.dao.DateDao;
import com.xuan.temper.entity.ExponentialInfo;
import com.xuan.temper.entity.RespBean;
import com.xuan.temper.entity.ReturnData;
import com.xuan.temper.service.ExponentialInfoService;
import com.xuan.temper.utils.dateUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @创建人： king xuan
 * @创建时间：2020/8/22
 * @描述：
 */
@RequestMapping("/test")
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {


    @Autowired
    private ExponentialInfoService exponentialInfoService;

    @Autowired
    private CodeTemplController codeTempController;

    @Autowired
    private DateDao dateDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    private static Logger log= LoggerFactory.getLogger(SaticScheduleTask.class);

    //每天下午15点30分清除redis（非交易时间不清除）
    @Scheduled(cron = "5,6 30 15 * * ?")
    @RequestMapping("/delete")
    public void taskDeleteRedis(){
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        log.info("执行静态定时任务时间: " + LocalDateTime.now());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String todayDate = dateFormat.format(date);
        System.out.println("当前时间是：" + todayDate);
        com.xuan.temper.entity.Date currenTime = dateDao.load(todayDate);
        Boolean holiday = currenTime.getHoliday();
        if (!holiday) {
            Set<String> keys = stringRedisTemplate.keys("*");
            // 批量获取数据
            for (String key : keys) {
                String s1 = key.toString();
                for (int i = 0; i < 10; i++) {
                    Boolean delete = stringRedisTemplate.delete(key);
                    Boolean delete1 = stringRedisTemplate.delete(s1);
                    if ( delete) {
                        System.out.println("清除成功");
                        log.info("清除成功");
                    } else {
                        System.out.println("清除失败");
                        log.info("清除失败");
                    }
                    if ( delete1) {
                        System.out.println("清除成功");
                        log.info("清除成功");
                    } else {
                        System.out.println("清除失败");
                        log.info("清除失败");
                    }
                }
            }
        }
    }

    //3.添加定时任务
    @Scheduled(cron = "0 0/5 * * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
    private void configureTasks() throws ParseException {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        log.info("执行静态定时任务时间: " + LocalDateTime.now());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String todayDate = dateFormat.format(date);
        System.out.println("当前时间是：" + todayDate);
        com.xuan.temper.entity.Date currenTime = dateDao.load(todayDate);
        Boolean holiday = currenTime.getHoliday();
       int i = 1;
       if (holiday) {
           while (true) {
               SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
               Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
               calendar.add(Calendar.DAY_OF_MONTH, -i); //当前时间减去一天，即一天前的时间
               Date resDate = calendar.getTime();
               String s = format.format(resDate);
               com.xuan.temper.entity.Date load1 = dateDao.load(s);
               if (!load1.getHoliday()) {
                   String day = load1.getDate();
                   task(day, 10);
                   break;
               }
               i++;
           }
        }else {
            System.out.println("当前时间是：" + currenTime.getDate()+" "+ currenTime.getWeek()+" "+currenTime.getHoliday());
            task(todayDate, 10);
        }
    }



    @RequestMapping("/add")
    public  RespBean task(String todayDate,Integer year) throws ParseException {
        ArrayList<String> list = new ArrayList<>();
        list.add("000016");
        list.add("000905");
        list.add("000300");
        list.add("399330");
        list.add("000036");
        list.add("399006");
        list.add("000922");
        list.add("000015");
        list.add("000807");
        list.add("000808");
        list.add("399967");
//        String years = dateUtils.getYears(year);
        String tenYears = dateUtils.getTenYears(todayDate);
        for (String stockCodes : list) {
            ReturnData data = codeTempController.codeTempController( tenYears,todayDate, stockCodes, "default");
            if (data != null){
                ExponentialInfo stockCodeName = exponentialInfoService.queryByStockCode(stockCodes);
                System.out.println("获取代码为：" + stockCodes + "名称为:" + stockCodeName + "的指数信息，以及计算指数温度，成功！！！");
            }else {
                ExponentialInfo stockCodeName = exponentialInfoService.queryByStockCode(stockCodes);
                System.out.println("获取代码为：" + stockCodes + "名称为:" + stockCodeName.toString() + "的指数信息，以及计算指数温度，失败！！！");
            }
        }
        return new RespBean("200","定时任务已执行","success");
    }

}
